﻿(function () {
    "use strict";

    var appView = Windows.UI.ViewManagement.ApplicationView;
    var appViewState = Windows.UI.ViewManagement.ApplicationViewState;

    WinJS.UI.Pages.define("/pages/home/home.html", {

        //Function for navigation when an album is clicked
        itemInvoked: function (args) {
            WinJS.Navigation.navigate("/pages/teams/teams.html");
        },

        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.
            Data.stats();

            var listView = element.querySelector(".albumsListView").winControl;
            listView.itemTemplate = element.querySelector(".albumTemplate");

            var mobileService = new WindowsAzure.MobileServiceClient("https://stickeralbum.azure-mobile.net/", "kykxDEEXBFMtYzPyBJdwTAHlXtwiNl95");
            try {
                mobileService.getTable("League").read().done(function (results) {
                    Data.albumArr = new WinJS.Binding.List(results);
                    listView.itemDataSource = Data.albumArr.dataSource;
                });
            } catch (exception) {
                var msg = new Windows.UI.Popups.MessageDialog("This application requires an internet connection");
                msg.showAsync();
            }

            //========== For the Duplicates AppBar ++++++++++++++
            var doublesListView = element.querySelector(".doublesLV").winControl;
            doublesListView.itemTemplate = element.querySelector(".doublesTemplate");

            var groupedItems = Data.duplikatiArr;

            doublesListView.itemDataSource = Data.getGrouped().dataSource;

            doublesListView.layout = new WinJS.UI.GridLayout();
            doublesListView.layout.maxRows = 1;

            //++++++++++++++++++++++++++++++++++++++++++++

            if (appView.value === appViewState.snapped) {
                listView.layout = new WinJS.UI.ListLayout();
            } else {
                listView.layout = new WinJS.UI.GridLayout();
            };

            listView.oniteminvoked = this.itemInvoked.bind(this);

            //============== OGNEN ==============================
            var doublesLV = element.querySelector('.doublesLV').winControl;
            doublesLV.forceLayout();
            function getSelected() {
                var tempList = [];
                var selectionCount = doublesLV.selection.count();
                if (selectionCount > 0 && selectionCount < 11) {
                    if (Data.checkClick("gameSticker", 2)) {
                        var indexes = [];//doublesLV.selection.getIndices();
                        //paketFlyout.filter(indexes, Data.duplikatiArr.length);

                        doublesLV.selection.getItems().done(function (currentSelection) {
                            currentSelection.forEach(function (selectedItem) {
                                gamePage.inputArr.push({ tim: selectedItem.data.tim, ime: selectedItem.data.ime, pozicija: selectedItem.data.pozicija, broj: selectedItem.data.broj, slika: selectedItem.data.slika });
                                Data.duplikatiArr.splice(Data.duplikatiArr.indexOfKey(selectedItem.key),1);
                            })
                        });

                        WinJS.Navigation.navigate("/pages/game/game.html", { selectionCount: selectionCount });
                    }
                    else {
                        var msg = new Windows.UI.Popups.MessageDialog("You can play the game every two hours");
                        msg.showAsync();
                    }
                }
                else {
                    var msg = new Windows.UI.Popups.MessageDialog("You have to choose at least one duplicate sticker or ten at most");
                    msg.showAsync();
                }
            }
            element.querySelector('#btnNewGame').addEventListener("click", getSelected, false);
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />
            /// <param name="viewState" value="Windows.UI.ViewManagement.ApplicationViewState" />
            /// <param name="lastViewState" value="Windows.UI.ViewManagement.ApplicationViewState" />

            var listView = element.querySelector(".albumsListView").winControl;
            if (lastViewState !== viewState) {
                if (appView.value === appViewState.snapped) {
                    listView.layout = new WinJS.UI.ListLayout();
                } else {
                    listView.layout = new WinJS.UI.GridLayout();
                }
            }
        }
    });
})();
